package config

import (
	"fmt"

	"gitee.com/smewl13/registry-sync/v2/logger"
	"gitee.com/smewl13/toolkit"
	"github.com/spf13/viper"
)

type RegistryServer struct {
	Addr     string `mapstructure:"address"`
	Ca       string `mapstructure:"ca"`
	User     string `mapstructure:"user"`
	Password string `mapstructure:"password"`
	Tls      bool   `mapstructure:"tls"`
}

var (
	Interval           int
	Source             = &RegistryServer{}
	Targets            = []*RegistryServer{}
	Log                = &logger.Log{}
	RootPath, ProcName = toolkit.GetRootPath()
)

func LoadConfig(cfgFile string) {
	viper.SetConfigFile(cfgFile)
	viper.SetConfigType("toml")
	viper.AddConfigPath(".")
	viper.AddConfigPath(RootPath)
	if err := viper.ReadInConfig(); err != nil {
		msg := fmt.Sprintf("读取配置文件失败: %v", err)
		panic(msg)
	}

	Interval = viper.GetInt("interval")
	viper.UnmarshalKey("source", Source)
	viper.UnmarshalKey("target", &Targets)
	viper.UnmarshalKey("log", Log)
}
